package com.blog.cloud.api.vo;

import com.blog.cloud.api.enums.HttpCode;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;

import java.io.Serial;
import java.io.Serializable;

@Getter
@Setter
@Accessors(chain = true)
public class ResultVo<T> implements Serializable {
    @Serial
    private static final long serialVersionUID = 1L;

    private int code;
    private String msg;
    private T data;

    public static <T> ResultVo<T> ok() {
        return restResult(null, HttpCode.OK.getCode(), HttpCode.OK.getMsg());
    }

    public static <T> ResultVo<T> ok(T data) {
        return restResult(data, HttpCode.OK.getCode(), HttpCode.OK.getMsg());
    }

    public static <T> ResultVo<T> ok(String msg) {
        return restResult(null, HttpCode.OK.getCode(), msg);
    }

    public static <T> ResultVo<T> ok(T data, String msg) {
        return restResult(data, HttpCode.OK.getCode(), msg);
    }

    public static <T> ResultVo<T> failed() {
        return restResult(null, HttpCode.NOT_FOUND.getCode(), HttpCode.NOT_FOUND.getMsg());
    }

    public static <T> ResultVo<T> failed(String msg) {
        return restResult(null, HttpCode.NOT_FOUND.getCode(), msg);
    }

    public static <T> ResultVo<T> failed(T data) {
        return restResult(data, HttpCode.NOT_FOUND.getCode(), HttpCode.NOT_FOUND.getMsg());
    }

    public static <T> ResultVo<T> failed(T data, String msg) {
        return restResult(data, HttpCode.NOT_FOUND.getCode(), msg);
    }

    public static <T> ResultVo<T> failed(T data, int code, String msg) {
        return restResult(data, code, msg);
    }

    public static <T> ResultVo<T> restResult(T data, int code, String msg) {
        ResultVo<T> response = new ResultVo<>();
        response.setData(data);
        response.setCode(code);
        response.setMsg(msg);
        return response;
    }
}
